package com.wjjun.base.ui.activity

import android.view.View
import androidx.annotation.UiThread
import androidx.databinding.ViewDataBinding
import com.wjjun.base.BR
import com.wjjun.base.R
import com.wjjun.base.databinding.ActivityLazyBinding
import com.wjjun.base.ui.viewmodel.BaseActivityViewModel
import com.wjjun.base.ui.viewmodel.BaseWindowViewModel
import com.wjjun.base.widget.status.Status
import com.wjjun.base.widget.status.StatusViewHelper
import com.wjjun.base.widget.status.viewmodel.StatusPagerViewModel

/**
 * 延迟加载的Activity
 * author : wangjunjun
 * date   : 2022/6/8
 * desc   :
 */
abstract class BaseLazyActivity<VM : BaseActivityViewModel, VD : ViewDataBinding> :
    BaseBindingActivity<VM, ActivityLazyBinding>(), StatusViewHelper.StatusViewHelperListener {

    protected val statusViewHelper = StatusViewHelper()

    override fun getLayoutId(): Int {
        return R.layout.activity_lazy
    }

    override fun initViews() {
        super.initViews()
        statusViewHelper.setLifeCleOwner(this)
        statusViewHelper.listener = this
        statusViewHelper.setStatsView(viewDataBinding.statusView)
        statusViewHelper.setSuccessViewModel(viewModel)

        statusViewHelper.showLoading()
    }

    /**
     * 结束加载
     */
    fun finishLoad() {
        runOnUiThread {
            statusViewHelper.showSuccess()
        }
    }


    override fun bindViews(
        viewDataBinding: ViewDataBinding,
        status: Status,
        viewModel: BaseWindowViewModel
    ) {
        viewDataBinding.setVariable(BR.viewModel, viewModel)
        viewDataBinding.setVariable(BR.listener, this)
        if (viewModel === this.viewModel) {
            initLazyContent(viewDataBinding as VD)
        }
    }


    /**
     * 初始化成功后的界面
     */
    abstract fun initLazyContent(viewDataBinding: VD)

    override fun onStatusPageClick(view: View, viewModel: StatusPagerViewModel) {
        when (viewModel.status) {
            Status.EMPTY, Status.ERROR -> {
                onRefresh(viewModel)
            }
            Status.PERMISSION -> {
                onRequestPermission(viewModel)
            }
            else -> {
                onPageClick(view, viewModel)
            }
        }
    }

    override fun onFetchDataError(e: Throwable) {
        super.onFetchDataError(e)
        showErrorPage()
    }

    /**
     * 错误界面或空界面刷新
     */
    open fun onRefresh(viewModel: BaseWindowViewModel) {
        statusViewHelper.showLoading()
        initData()
    }

    /**
     * 重新请求权限
     */
    open fun onRequestPermission(viewModel: BaseWindowViewModel) {
        initData()
    }

    /**
     * 自定义状态的点击事件
     */
    open fun onPageClick(view: View, viewModel: StatusPagerViewModel) {

    }

    fun showErrorPage() {
        statusViewHelper.showError()
    }

    fun setErrorPage(icon: Int, msg: String) {
        statusViewHelper.setError(icon, msg)
    }
    fun showEmptyPage() {
        statusViewHelper.showEmpty()
    }

    fun setEmptyPage(icon: Int, msg: String) {
        statusViewHelper.setEmpty(icon, msg)
    }

    fun setPermissionPage(icon: Int, msg: String) {
        statusViewHelper.setPermission(icon, msg)
    }

    fun addStatusPage(status: Status, viewModel: BaseWindowViewModel) {
        statusViewHelper.addStatusViewModel(status, viewModel)
    }

}